import books.to.africa.*

class BootStrap {
 def springSecurityService
    def init = { 
 
 def teacherRole = Role.findByAuthority('ROLE_TEACHER') ?: new Role(authority: 'ROLE_TEACHER').save(failOnError: true)
 def donorRole = Role.findByAuthority('ROLE_DONOR') ?: new Role(authority: 'ROLE_DONOR').save(failOnError: true)
def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
        def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
 def adminUser = 
	User.findByUsername('admin') ?: 
		new User(
                username: 'admin',
                password: 'admin',
                enabled: true,
//description: 'System Administrator',
//accountExpired: false,
//accountLocked: false,
//passwordExpired: false
).save(failOnError: true)

def derpMaster = 
	User.findByUsername('derp') ?: 
		new DonorRegister(
		email: 'dur@hotmail.com',
		username: 'derp',
		password: 'master',
		enabled: true).save(failOnError: true)
			
//UserRole.create derpMaster, userRole
//UserRole.create adminUser, adminRole apparently something fundamentally wrong about using name admin and password admin... maybe because it's a dumb username/password combination....

def normalUserTest = 
	User.findByUsername('Norman') ?:
		new User(
			username: 'Norman',
			password: "normal",
			enabled: true).save(failOnError: true)
	UserRole.create normalUserTest, userRole

if (!derpMaster.authorities.contains(adminRole)) {
            UserRole.create derpMaster, adminRole
        }
//assert User.count == 3
//assert Role.count == 2
//servletContext ->
    }
    def destroy = {
    }
}
